package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageManager {

    @Reference
    private UserInfoService userInfoService;

    /**
     * 分页查询公告列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageBeanVo announcementList(Integer pageNumber, Integer pageSize) {
        // 1、调用api实现分页查询
        PageBeanVo pageBeanVo = userInfoService.announcementList(pageNumber, pageSize);

        // 2、封装vo对象
        List<AnnouncementVo> voList = new ArrayList<>();
        List<Announcement> announcementList = (List<Announcement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(announcementList)) {
            for (Announcement announcement : announcementList) {
                AnnouncementVo vo = new AnnouncementVo();
                BeanUtil.copyProperties(announcement, vo);
                vo.setCreateDate(DateUtil.format(announcement.getCreated(), "yyyy-MM-dd HH:mm:ss").toString());
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }
    }
